package cn.mumzone.basic.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 客户端日期工具类 提供常用时间日期处理
 *
 * @author joe
 * @version 2015.03.26
 */
public class AppDateUtil {

    /**
     * 日期格式：yyyy-MM-dd
     */
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    /**
     * 日期格式：yyyyMMdd
     */
    public static final String YYYYMMDD = "yyyyMMdd";
    /**
     * 日期格式：yyyy-MM-dd
     */
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    /**
     * 日期格式：yy-MM-dd
     */
    public static final String YY_MM_DD = "yy-MM-dd";

    /**
     * 日期格式：HH:mm
     */
    public static final String HH_MM = "HH:mm";

    /**
     * 日期格式：HH:mm
     */
    public static final String HH_MM_SS = "HH:mm:ss";
    /**
     * 日期格式：MM/dd HH:mm:ss
     */
    public static final String MM_DD_HH_MM_SS = "MM/dd HH:mm:ss";
    /**
     * 日期格式：yyyy-MM-dd HH:mm
     */
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

    /**
     * 日期格式：yyyy-MM-dd HH:mm:ss
     */
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    /**
     * 日期格式：yyyy-MM-ddHH:mm:ss
     */
    public static final String YYYY_MM_DDHH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    /**
     * 日期格式：MM-dd HH:mm
     */
    public static final String MM_DD_HH_MM = "MM-dd HH:mm";

    /**
     * 日期格式：MM-dd
     */
    public static final String MM_DD = "MM-dd";

    /**
     * 日期格式：MM月 dd日 HH:mm
     */
    public static final String MM_DD1 = "MM月dd日";

    public static final String MM_DD2 = "MM.dd";
    /**
     * 日期格式：MM月 dd日 HH:mm
     */
    public static final String MM_DD_HH_MM1 = "MM月dd日 HH:mm";

    /**
     * 日期格式：YY年 MM月 dd日 HH:mm
     */
    public static final String YYYY_MM_DD_HH_MM1 = "yyyy年MM月dd日 HH:mm";

    public static final String YYYY_MM_DD_HH_MM2 = "yyyy.MM.dd HH:mm";

    /**
     * 日期格式：YY年 MM月 dd日
     */
    public static final String YYYY_MM_DD1 = "yyyy年MM月dd日";


    public static final String YYYY_MM_DD2 = "yyyy.MM.dd";

    public static final String MM_DD_HH_MM2 = "MM.dd HH:mm";

    /**
     * 根据long值，转化为yyyy-MM-dd hh:mm的格式
     *
     * @param timelong
     * @return String
     */
    public static String getTimeStamp(long timelong) {
        Date d = new Date(timelong);
        SimpleDateFormat df = new SimpleDateFormat(YYYY_MM_DD_HH_MM);
        return df.format(d);
    }

    /**
     * timepatten就是SimpleDateFormat用到的patten，比如："yyyy-MM-dd HH:mm"
     *
     * @param timelong
     * @param timepatten
     * @return String
     */
    public static String getTimeStamp(long timelong, String timepatten) {
        Date d = new Date(timelong);
        SimpleDateFormat df = new SimpleDateFormat(timepatten);
        return df.format(d);
    }

    /**
     * timepatten就是SimpleDateFormat用到的patten，比如："yyyy-MM-dd HH:mm"
     *
     * @param timelong
     * @param timepatten
     * @return String
     */
    public static String getTimeStamp(Long timelong, String timepatten) {
        if (null == timelong) {
            return "";
        }
        Date d = new Date(timelong);
        SimpleDateFormat df = new SimpleDateFormat(timepatten);
        return df.format(d);
    }

    /**
     * timepatten就是SimpleDateFormat用到的patten，比如："yyyy-MM-dd HH:mm"
     *
     * @param date
     * @param timepatten
     * @return String
     */
    public static String getTimeStamp(Date date, String timepatten) {
        SimpleDateFormat df = new SimpleDateFormat(timepatten);
        return df.format(date);
    }

    /**
     * 根据年月日的值取得时间片
     *
     * @param mYear
     * @param mMonth
     * @param mDay
     * @return long
     */
    public static long getTimeStampFromYearMonthDay(int mYear, int mMonth,
                                                    int mDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, mYear);
        calendar.set(Calendar.MONTH, mMonth);
        calendar.set(Calendar.DAY_OF_MONTH, mDay);
        return calendar.getTimeInMillis();
    }

    /**
     * 解析字符串，返回Date对象，字符串格式默认为yyyy-MM-dd；如果解析出现异常，返回null。
     *
     * @param strDate
     * @return Date
     */
    public static Date parseDate(String strDate) {
        return parseDate(strDate, YYYY_MM_DD);
    }

    /**
     * 解析字符串，返回Date对象，可以传入字符串格式进行解析，建议使用DateUtil内置的字符串格式。如果解析出现异常，返回null。
     *
     * @param strDate
     * @return Date
     */
    public static Date parseDate(String strDate, String dateFormat) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 根据年月日的值取得时间片
     *
     * @param mYear
     * @param mMonth
     * @param mDay
     * @return long
     */
    public static long getTimeStampFromYearMonthDay(int mYear, int mMonth,
                                                    int mDay, int mHour, int mMin) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, mYear);
        calendar.set(Calendar.MONTH, mMonth);
        calendar.set(Calendar.DAY_OF_MONTH, mDay);
        calendar.set(Calendar.HOUR, mHour);
        calendar.set(Calendar.MINUTE, mMin);
        return calendar.getTimeInMillis();
    }

    /**
     * 比较传入时间是否比当前小
     *
     * @param year
     * @param month
     * @param dayOfMonth
     * @return
     */
    public static boolean compareDateIsLessThanNow(int year, int month,
                                                   int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        int nowYear = calendar.get(Calendar.YEAR);
        int nowMonth = calendar.get(Calendar.MONTH) + 1;
        int nowDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        if (year < nowYear) {
            return true;
        }
        if (year > nowYear) {
            return false;
        }
        if (month < nowMonth) {
            return true;
        }
        if (month > nowMonth) {
            return false;
        }
        if (dayOfMonth < nowDayOfMonth) {
            return true;
        }
        return false;
    }

    /**
     * 比较传入时间是否比当前小
     *
     * @param timeStamp
     * @return
     */

    public static boolean compareDateIsLessThanNow(String timeStamp) {
        return Long.valueOf(timeStamp) < System.currentTimeMillis();
    }

    /**
     * 获取日期的开始时间
     *
     * @param date
     */
    public static long getDayStartTime(Date date) {
        if (null == date) {
            date = new Date();
        }
        //设置系统时区为北京时区，否则会导致时间和服务器时间不一致
        Calendar startCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        startCalendar.setTime(date);
        String formatDate = String.format("%s-%s-%s %s:%s:%s",
                startCalendar.get(Calendar.YEAR), startCalendar.get(Calendar.MONTH) + 1,
                startCalendar.get(Calendar.DAY_OF_MONTH),
                "00", "00", "00");

        return getStringToDate(formatDate, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取日期的截止时间
     *
     * @param date
     * @return
     */
    public static long getDayEndTime(Date date) {
        if (null == date) {
            date = new Date();
        }
        //设置系统时区为北京时区，否则会导致时间和服务器时间不一致
        Calendar startCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        startCalendar.setTime(date);
        String formatDate = String.format("%s-%s-%s %s:%s:%s",
                startCalendar.get(Calendar.YEAR), startCalendar.get(Calendar.MONTH) + 1,
                startCalendar.get(Calendar.DAY_OF_MONTH),
                "23", "59", "59");

        return getStringToDate(formatDate, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * * 日期格式字符串转换成时间戳
     *
     * @param
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 判断是否是同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isPreviousDate(Date date1, Date date2) {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(date1);

        Calendar calDateB = Calendar.getInstance();
        calDateB.setTime(date2);

        return calDateA.get(Calendar.YEAR) >= calDateB.get(Calendar.YEAR)
                && calDateA.get(Calendar.MONTH) >= calDateB.get(Calendar.MONTH)
                && calDateA.get(Calendar.DAY_OF_MONTH) >= calDateB
                .get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 判断是否是同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2) {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(date1);

        Calendar calDateB = Calendar.getInstance();
        calDateB.setTime(date2);

        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
                && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
                && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB
                .get(Calendar.DAY_OF_MONTH);
    }


    /**
     * 判断是否生日
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isBirthdayDay(Date date1, Date date2) {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(date1);

        Calendar calDateB = Calendar.getInstance();
        calDateB.setTime(date2);

        return calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH) && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 判断是否是同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDay(Calendar date1, Calendar date2) {
        if (null == date1 || date2 == null) {
            return false;
        }
        Calendar calDateA = date1;
        Calendar calDateB = date2;
        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
                && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
                && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB
                .get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 是不是周末
     *
     * @param date
     * @return
     */
    public static boolean isWeekend(Date date) {
        if (null == date) {
            return false;
        }
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(date);
        return calDateA.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calDateA.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
    }

    /**
     * 获取今天是周几
     *
     * @param dt
     * @return
     */
    public static String getWeekOfDate(long dt) {

        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(dt));

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }

    public static String getDateToString(long milSecond, String pattern) {
        Date date = new Date(milSecond);
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    public static long getStringToDate(String dateString, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date date = new Date();
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static Date timeStamp2Date(String timeStamp) {
        return new Date(Long.valueOf(timeStamp));
    }

    public static long getNextDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        String date = String.format("%s-%s-%s", calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
        return Long.valueOf(date2TimeStamp(date, YYYY_MM_DD));
    }

    public static long getNextDay(long currentDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTimeInMillis(currentDay);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        String date = String.format("%s-%s-%s", calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
        return Long.valueOf(date2TimeStamp(date, YYYY_MM_DD));
    }

    public static long getPreviousDay(long currentDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTimeInMillis(currentDay);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String date = String.format("%s-%s-%s", calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
        return Long.valueOf(date2TimeStamp(date, YYYY_MM_DD));
    }


    /**
     * 计算2个日期之间相差的  相差多少年月日
     * 比如：2011-02-02 到  2017-03-02 相差 6年，1个月，0天
     *
     * @return
     */
    public static String dayComparePrecise(long fromTimeMillis) {
        Date fromDate = new Date(fromTimeMillis);
        Calendar from = Calendar.getInstance();
        from.setTime(fromDate);
        Date toDate = new Date();
        Calendar to = Calendar.getInstance();
        to.setTime(toDate);

        int fromYear = from.get(Calendar.YEAR);
        int fromMonth = from.get(Calendar.MONTH);
        int fromDay = from.get(Calendar.DAY_OF_MONTH);

        int toYear = to.get(Calendar.YEAR);
        int toMonth = to.get(Calendar.MONTH);
        int toDay = to.get(Calendar.DAY_OF_MONTH);
        int year = toYear - fromYear;
        int month = toMonth - fromMonth;
        int day = toDay - fromDay;
        day = day + 1;
        if (year == 0) {
            if (month == 0) {
                return day + "天";
            } else {
                return month + "个月" + day + "天";
            }
        } else {
            return year + "岁" + month + "个月" + day + "天";
        }
    }


    public static String getFriendlyDateTime(String date,String timeFormat ,String dateFormat) {
        Calendar currentDate = Calendar.getInstance();
        currentDate.setTime(new Date());

        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(AppDateUtil.timeStamp2Date(date));

        if (currentDate.get(Calendar.YEAR) != dateCalendar.get(Calendar.YEAR) || currentDate.get(Calendar.MONTH) != dateCalendar.get(Calendar.MONTH)) {
            return AppDateUtil.getTimeStamp(Long.valueOf(date), YYYY_MM_DD_HH_MM_SS);
        }

        int currentDay = currentDate.get(Calendar.DAY_OF_MONTH);
        int day = dateCalendar.get(Calendar.DAY_OF_MONTH);

        int diffDay = currentDay - day;

        if (diffDay == 1) {//昨天
            return String.format("昨天 %s", AppDateUtil.getTimeStamp(Long.valueOf(date), timeFormat));
        } else if (diffDay == 0) {//今天
            return String.format("今天 %s", AppDateUtil.getTimeStamp(Long.valueOf(date), timeFormat));
        } else if (diffDay == -1) {//明天
            return String.format("明天 %s", AppDateUtil.getTimeStamp(Long.valueOf(date), timeFormat));
        } else if (diffDay == -2) {//后天
            return String.format("后天 %s", AppDateUtil.getTimeStamp(Long.valueOf(date), timeFormat));
        } else {
            return AppDateUtil.getTimeStamp(Long.valueOf(date), dateFormat);
        }
    }


    public static boolean compareDate(Date fromDate,Date toDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(fromDate);
        int fromYear = fromCalendar.get(Calendar.YEAR);
        int fromMonth = fromCalendar.get(Calendar.MONTH) + 1;
        int fromDayOfMonth = fromCalendar.get(Calendar.DAY_OF_MONTH);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(toDate);
        int toYear = toCalendar.get(Calendar.YEAR);
        int toMonth = toCalendar.get(Calendar.MONTH) + 1;
        int toDayOfMonth = toCalendar.get(Calendar.DAY_OF_MONTH);
        if (toYear > fromYear) {
            return true;
        }
        if (fromYear > toYear) {
            return false;
        }
        if (toMonth > fromMonth) {
            return true;
        }
        if (fromMonth > toMonth) {
            return false;
        }
        if (toDayOfMonth > fromDayOfMonth) {
            return true;
        }
        if (fromDayOfMonth > toDayOfMonth) {
            return false;
        }
        return false;
    }
}
